const express = require('express');
const app = express();
const jwt = require('express-jwt');
app.listen(3007, () => { console.log('来了来了') });
const cors = require('cors');
app.use(cors());
// app.use(express.urlencoded({ extended: false }));
// app.use(express.urlencoded({ extended: false }))
app.use(express.urlencoded({ extended: false }));
app.use(express.static('./uploads'));
const path = require('path');
app.use(jwt({ secret: 'bigevent' }).unless({ path: /^\/api/ }))
    // app.use(express_jwt({ secret: 'bigevent' }).unless({ path: /^\/api/ }));
app.use('/api', require(path.join(__dirname, 'routers', 'login')));
app.use('/my/article', require(path.join(__dirname, 'routers', 'category')));
app.use('/my/article', require(path.join(__dirname, 'routers', 'article')));
app.use('/my', require(path.join(__dirname, 'routers', 'user')));


// 错误处理中间件
// 错误处理中间件，必须是4个参数
app.use(function(err, req, res, next) {
    // console.log(err.name); // 错误的名字
    // console.log(err.message); // 错误的提示
    if (err.name === 'UnauthorizedError') {
        console.log(err.message); // 错误的提示
        // 如果错误的名字是 UnauthorizedError，则表示是token相关的错误
        res.status(401).send({ status: 1, message: '身份认证失败！' });
    }
});